package com.liangplus.elasticsearch.controller;

import com.liangplus.elasticsearch.entity.eo.UserPEO;
import com.liangplus.elasticsearch.repository.ShrRepository;
import com.liangplus.elasticsearch.repository.UserPRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;


/**
 * Created by yumin on 2019/8/2.
 */
@Controller
@RequestMapping("/user")
public class UserPController {

    @Autowired
    private UserPRepository userPRepository;

    @PostMapping
    @ResponseBody
    public String add(@RequestBody UserPEO userPEO) {
        userPRepository.save(userPEO);
        return "success";
    }

    @DeleteMapping(value = "/{id}")
    @ResponseBody
    public String delete(@PathVariable Long id) {
        userPRepository.deleteById(id);
        return "success";
    }

    @GetMapping(value = "/{id}")
    @ResponseBody
    public UserPEO getById(@PathVariable Long id) {
        Optional<UserPEO> userPEOOptional = userPRepository.findById(id);
        if (userPEOOptional.isPresent()) {
            UserPEO userpeo = userPEOOptional.get();
            return userpeo;
        }
        return null;
    }


}
